package jkd.lrn.net.ams.core.codec;

import jkd.lrn.net.ams.core.message.MDRRequest;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoder;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;

public class MDRRequestDecoder implements MessageDecoder {

	@Override
	public MessageDecoderResult decodable(IoSession session, IoBuffer buffer) {
		
		if(buffer.remaining() < Constants.MDR_REQ_SIZE){
			return MessageDecoderResult.NEED_DATA;
		}else if(buffer.remaining() == Constants.MDR_REQ_SIZE){
			return MessageDecoderResult.OK;
		}
		
		return MessageDecoderResult.NOT_OK;
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer buffer,	ProtocolDecoderOutput out) throws Exception {
		
		if(buffer.remaining() < Constants.MDR_REQ_SIZE){
			return MessageDecoderResult.NEED_DATA;
		}else {
			final MDRRequest request = new MDRRequest();
			request.setMessageId(buffer.getInt());
			request.setEmployeeId(buffer.getInt());
			request.setMove(buffer.get());
			request.setTimeStamp(buffer.getLong());
			out.write(request);
			
			return MessageDecoderResult.OK;
		}
		
	}

	@Override
	public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
		// TODO Auto-generated method stub

	}

}
